//插件加载完毕后，会执行初始化化操作
// $(function () {
//     //检测 IE 中的插件
// 	WebVideoCtrl.insertPluginObject("divPlugin",500,300);
// 	//初始化插件
// 	WebVideoCtrl.initPlugin("Dahua2",function () {
// 			//创建视频窗口
// 			WebVideoCtrl.createMultiNodeDisplay(16);
// 			//设置视频窗口的显示
// 			var num = parseInt($("#wndNum").find("option:selected").val());
//         		num=1;
// 			//设置窗口分割数
// 			WebVideoCtrl.setSplitNum(num);
// 			//注册事件
// 			WebVideoCtrl.registerEvent("SelectedView",responseSelectedViewSignal);
// 			//隐藏窗口序号选择框
// 			$("#winIndex").hide();
// 			}
// 	);
//     clickLogin();
// });
var szIPPort = ''
var account = ''
var password = ''
var ichannel = ''
//插件加载完毕后，会执行初始化化操作
function doIt() {

    szIPPort = sessionStorage.getItem('szIPPort')
    account = sessionStorage.getItem('account')
    password = sessionStorage.getItem('password')
    ichannel = sessionStorage.getItem('ichannel')
    console.log(szIPPort,account,password,ichannel)

    //检测 IE 中的插件
    WebVideoCtrl.insertPluginObject("divPlugin",500,300);
    //初始化插件
    WebVideoCtrl.initPlugin("Dahua2",function () {
            //创建视频窗口
            WebVideoCtrl.createMultiNodeDisplay(16);
            //设置视频窗口的显示
            var num = parseInt($("#wndNum").find("option:selected").val());
            num=1;
            //设置窗口分割数
            WebVideoCtrl.setSplitNum(num);
            //注册事件
            WebVideoCtrl.registerEvent("SelectedView",responseSelectedViewSignal);
            //隐藏窗口序号选择框
            $("#winIndex").hide();
        }
    );
    clickLogin();
}
//视频窗口
/**
 *@description 处理窗口切换事件
 *@param{Num} iNodeIndex  节点序号
 *@param{Num} iViewIndex  视图序号
 *@param{Num} iWinID      窗口ID
 */
function responseSelectedViewSignal(iNodeIndex,iViewIndex,iWinID){
    //更新对应播放器的信息
    var playrInfo = WebVideoCtrl.getPlayerInfo(iWinID);
    //更新UI信息
    if(typeof playrInfo != "undefined"){
        //设备信息
        var deviceInfo = WebVideoCtrl.getDeviceInfo(playrInfo.ip);
        if(typeof deviceInfo != "undefined"){
            DemoUI.updateDeviceInfo(playrInfo.ip);
            DemoUI.setCurChannel(playrInfo.channle);
            DemoUI.setCurStreamType(playrInfo.streamType);
        }
    }
}

//显示操作信息
function showOPInfo(szInfo, status, error) {
    var szTip = "<div>" + Foundation.dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss") + " " + szInfo;
    if (typeof status != "undefined")
    {
        szTip += "(" + status.toString() + ", " + error.toString() + ")";
    }
    szTip += "</div>";
    $("#opinfo").html(szTip + $("#opinfo").html());
}

//窗口切换事件
function changeWndNum(num)
{
    //设置视频窗口的显示
    var num = parseInt($("#wndNum").find("option:selected").val());
    WebVideoCtrl.setSplitNum(num);
    DemoUI.setWinIndex(num);
}
//设备登录
function clickLogin()
{
    var szIPPortarr=szIPPort.split(":");
    var szIP = szIPPortarr[0];
    szPort = parseInt(szIPPortarr[1]);
    szUsername = account;
    szPassword = password;
    rtspPort =szPort;
    protocol =  0;  // 协议类型 tcp：0 udp：4 组播：3
    if ("" == szIP || "" == szPort) {
        return;
    }
    var port = szPort;
    console.log("判断当前设备是否已经登录");
    //判断当前设备是否已经登录
    var deviceInfo = WebVideoCtrl.getDeviceInfo(szIP);
    console.log("deviceInfo",deviceInfo);
    if(typeof deviceInfo != "undefined"){
        if(WebVideoCtrl.logout(szIP))
        {
            //添加提示
            console.log(szIP + " Logout Device ");
            //删除设备信息
            DemoUI.removeDeviceInfo(szIP);
        }
    }
    //port=37778;
    console.log(szIP+"--"+port+"--"+szUsername+"--"+szPassword);
    WebVideoCtrl.login(szIP,port,szUsername,szPassword,rtspPort,protocol,
        function(sIp,iDeviceID){
            console.log("iDeviceID"+iDeviceID);
            iDeviceID=3;

            //插入设备
            DemoUI.addDeviceIP(sIp);
            //获得通道号
            var channelNum = WebVideoCtrl.getChannelNumber(iDeviceID);
            //更新通道数据
            DemoUI.modifyChannelList(channelNum);
        },
        function(iErrorCode,sError){
            console.log(szIP + " Login Fail ", iErrorCode, sError);
        }
    );
    clickStartRealPlay(szIP);
}



function clickLogout(ip)
{
    //获取当前选中的
    //var ip = DemoUI.getCurDeviceIP();
    if(WebVideoCtrl.logout(ip))
    {
        //添加提示
        console.log(ip + " Logout Device ");
        //删除设备信息
        DemoUI.removeDeviceInfo(ip);
    }
}
// 点击预览
function clickStartRealPlay(szIP){
    //获得当前选中的设备IP
    var sIP = szIP;
    console.log("sIP",sIP);
    //获得通道号
    console.log(ichannel+" 通道号");
    var iChannel = ichannel;
    //获得码流类型
    var iStreamType = parseInt(1, 10);
    //窗口选择模式
    var iMode = parseInt(0, 10);
    if(0 == iMode){
        WebVideoCtrl.connectRealVideo(sIP,iChannel,iStreamType,function(iPlayerID){
                console.log(sIP + " Channel:"+ iChannel.toString() + " Live succeed");
            },
            function(status,error){
                console.log(sIP + " Channel:"+ iChannel.toString() + " Live Fail", status, error);
            }
        )
    }else{
        //窗口序号
        var iWinIndex = parseInt($("#winIndex").val(), 10);
        WebVideoCtrl.connectRealVideoEx(iWinIndex,sIP,iChannel,iStreamType,function(iPlayerID){
                console.log(sIP + " Channel:"+ iChannel.toString() + " Live succeed");
            },
            function(status,error){
                console.log(sIP + " Channel:"+ iChannel.toString() + " Live Fail", status, error);
            }
        )
    }
}
//关闭选中窗口的实时监视
function clickStopRealPlay(){
    WebVideoCtrl.closePlayer();
}
